package dawn.jxufe.community.actuator;

import dawn.jxufe.community.util.CommunityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author dawn21
 * @version 1.0
 * @date 2022/9/4 10:15
 * @component
 */
@Component
@Endpoint(id = "database")
public class DataBaseEndpoint {
    private static final Logger logger = LoggerFactory.getLogger(DataBaseEndpoint.class);

    @Autowired
    private DataSource dataSource;
    @ReadOperation
    public String checkConnection(){
        try (Connection conn = dataSource.getConnection()) { // 小括号里面的会自动放入finally
            return CommunityUtil.getJSONString(0,"获取链接成功！");
        } catch (SQLException e){
            logger.error("获取链接失败");
            return CommunityUtil.getJSONString(1,"获取链接失败！");
        }
    }
}

